package demo.web.ui.ctrl;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.validation.ConstraintViolation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.BindingValidateEvent;
import org.zkoss.zul.Label;

import demo.model.DAOs;
import demo.model.UserDAO;
import demo.model.bean.CartItem;
import demo.model.bean.User;
import demo.web.ShoppingCart;
import demo.web.ui.ctrl.Validations.ViolationRunner;

public class UserViewModel extends GenericForwardComposer {

	private String listUsers;
	private CartItem selectedItem;
	private UserDAO userDao = new UserDAO();
	private User selectedUser, newUser = new User();
	
	private static final long serialVersionUID = 2365609737947623977L;

	private final Logger _logger = LoggerFactory.getLogger(this.getClass());
	private User _user = User.getUser();

	// getter //
	public User getUser() {
		return _user;
	}

	// event handler //
	public void onBindingValidate$submit(BindingValidateEvent event) {

		Validations.validate(event, new ViolationRunner(){
			public void run(Set<ConstraintViolation<?>> violations, Object bean, Component ref) {

				// set or clear error labels
				Component nref = ref.getNextSibling();
				if(nref instanceof Label)
					((Label) nref).setValue(violations.isEmpty() ? 
							"" : violations.iterator().next().getMessage());

				// may also log here
				for(ConstraintViolation<?> v : violations)
					_logger.info("[Bean Constraint Violation] " + bean.getClass() + 
							": " + v.getPropertyPath() + ": " + v.getMessage()); 
			}
		});

		_logger.info("Data saved to user: " + _user.getFullDesc());
	}
	
	
	public String getListUsers() {
		return listUsers;
	}

	public void setListUsers(String listUsers) {
		this.listUsers = listUsers;
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	public User getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}

	public User getNewUser() {
		return newUser;
	}

	public void setNewUser(User newUser) {
		this.newUser = newUser;
	}
	
	public User getUserSession() {
		System.out.println(UserUtils.getUserSession().toString());
		return UserUtils.getUserSession();
	}

	@Command("addUser")
	@NotifyChange("users")
	public void addUser() {
		DAOs.getUserDAO().add(this.newUser);
		this.newUser = new User();
	}

	@Command("update")
	@NotifyChange("users")
	public void updateUser() {
		DAOs.getUserDAO().update(this.selectedUser);
	}

	@Command("delete")
	@NotifyChange({"users", "selectedUser"})
	public void deleteUser() {
		//shouldn't be able to delete with selectedUser being null anyway
		//unless trying to hack the system, so just ignore the request
		if(this.selectedUser != null) {
			DAOs.getUserDAO().remove(this.selectedUser);
			this.selectedUser = null;
		}
	}

}
